package org.happy.tester.catcher.controller;


import com.alibaba.fastjson.JSONObject;
import org.happy.tester.catcher.service.CollectService;
import org.happy.tester.common.http.req.CollectResultRequest;
import org.happy.tester.common.http.req.PointcutRequest;
import org.happy.tester.common.http.resp.BaseResponse;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Classname PointCut
 * @Description TODO
 * @Date 2020/4/5 07:15 下午
 * @Created by jc.bai
 */
@RestController
@RequestMapping("/catcher")
public class CollectController {

    @Resource
    private CollectService collectService;

    @GetMapping(value = "/collect/need/get")
    public BaseResponse<Boolean> needCollect(String pointcut) {
        System.out.println("/collect/need/get : " + pointcut);
        BaseResponse<Boolean> response = new BaseResponse<>();
        boolean needCollect = collectService.needCollect(pointcut);
        response.setData(needCollect);
        return response.success();
    }

    @PostMapping(value = "/collect/result/push")
    public void saveCollectResult(@ModelAttribute CollectResultRequest collectResultRequest) {
        System.out.println("/collect/result/push : " + JSONObject.toJSONString(collectResultRequest));
        collectService.saveCollectResult(collectResultRequest);
    }

    @PostMapping(value = "/collect/need/mark", produces = "application/json;charset=utf-8")
    public BaseResponse markCollect(@RequestBody List<PointcutRequest> pointcutList) {
        System.out.println("/collect/need/mark : " + JSONObject.toJSONString(pointcutList));
        collectService.markCollect(pointcutList);
        return new BaseResponse().success();
    }

    @GetMapping(value = "/collect/result/fetch")
    public BaseResponse<String> fetchCollectResult(String pointcut) {
        System.out.println("/collect/result/fetch : " + pointcut);
        BaseResponse<String> baseResponse = new BaseResponse();
        String result = collectService.fetchCollectResult(pointcut);
        baseResponse.setData(result);
        return baseResponse.success();
    }
}
